package cn.tedu.v6;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 多人聊天室：Version 6  登录   退出：退两次
 *
 */

public class Client {
    private static final String HOST="127.0.0.1";
    private static final Integer PORT=9908;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;


    public Client(){
        try {
            socket=new Socket(HOST,PORT);
            ip=socket.getInetAddress().getHostAddress();

            br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF8"));
            pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8")),true);


        }catch (UnknownHostException e) {
            System.out.println("IP地址有问题，请询问服务器端");
        }catch (SocketException e) {
            System.out.println("链接被重置");
        }
        catch (IOException e) {
            System.out.println("客户端主动退出了【断开连接】");
        }
    }

    public void start(){
        System.out.println("~~~~~欢迎使用晴天牌QQ聊天室~~~~~");

        try {
            Scanner input=new Scanner(System.in);
            String severMessage=br.readLine();
            System.out.println(severMessage);
            String username=input.nextLine();
            pw.println(username);
            severMessage=br.readLine();
            System.out.println(severMessage);
            String password=input.nextLine();
            pw.println(password);


            severMessage=br.readLine();
            System.out.println(severMessage);

            if (severMessage.startsWith("登陆成功")){
                chat();
            }

        }catch (IOException e){
            System.out.println("客户端断开连接！！！");
        }
        finally {

            if(pw!=null)
                pw.close();

            if (br!=null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯内容失败");
                }

            if (socket!=null&&socket.isClosed()){
                System.out.println("客户端关闭成功");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            }
        }
    }
    private void chat(){
        new Thread(()->{
            try {
                String message;
                while ((message=br.readLine())!=null){
                    System.out.println("服务器说："+message);
                    System.out.print("客户端请输入：");
                    if (message.equalsIgnoreCase("exit")){

                        System.exit(0);
                    }
                }
            } catch (IOException e) {
                System.out.println("服务器端发来消息断开");
            }
        }).start();

        Scanner input=new Scanner(System.in);
        while (true){
            System.out.println("客户端请输入：");
            String say=input.nextLine();
            pw.println("客户端说："+say);
           if (say.equalsIgnoreCase("exit")){
               System.out.println("退了吧，拜拜！！！");
               break;

           }
        }
    }

    public static void main(String[] args) {
       new  Client().start();
    }
}